﻿#region

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using JCore.LoLChat;
using JCore.LoLProfiler_Test.Classes.API;
using JCore.LoLProfiler_Test.Classes.Objects;
using LoL_Profiler;
using Friend = JCore.LoLProfiler_Test.Classes.Objects.Friend;

#endregion

namespace JCore.LoLProfiler_Test.Classes
{
    public class ObjectConvertors
    {
        public Match GameStatisticsToMatchObject(string ownSummonerName,
                                                        ElophantAPI.RecentGames.GameStatistic GameStatistics,
                                                        List<ElophantAPI.RecentGames.FellowPlayer> PremadeFriends, DatabaseGeneration newDatabase = null)
        {
            
            Match match = new Match();
            List<Player> tempFriendsList = new List<Player>();
            match.Ranked = GameStatistics.ranked;
            match.GameMode = GameStatistics.gameMode;
            match.SubType = GameStatistics.gameType;
            if (GameStatistics.statistics.FirstOrDefault(f => f.statType == "WIN") == null)
            {
                match.Won = false;
            }
            else
            {
                match.Won = true;
            }
            if (GameStatistics.teamId == 100)
            {
                match.Side = "Blue";
            }
            else
            {
                match.Side = "Purple";
            }

            // For Each friend in the already cleaned-up list...
            foreach (ElophantAPI.RecentGames.FellowPlayer friend in PremadeFriends)
            {
                Player player = new Player {Name = friend.summonerName, Champion = friend.championId};
                Friend friendInfo = new Friend();
                if (newDatabase != null)
                {
                    if (newDatabase.newDatabase != null)
                    {
                        friendInfo =
                            newDatabase.newDatabase.SummonerInfo.Friends.FirstOrDefault(
                                              f => f.Name == friend.summonerName);
                        if (friendInfo != null && friendInfo.AccountId == 0)
                        {
                            friendInfo.AccountId = JSONdownloader.SummonerProfile(player.Name).acctId;
                            // Add to database
                            // works, but it doesnt use the loaded database, quite stupid
                            newDatabase.newDatabase.SummonerInfo.Friends.First(w => w.Name == friend.summonerName)
                                          .AccountId =
                                friendInfo.AccountId;
                        }
                    }
                    else
                    {
                        friendInfo.AccountId = JSONdownloader.SummonerProfile(player.Name).acctId;
                    }
                }
                else
                {
                    if (frmGetInfoTest.getInstance().database != null)
                    {
                        // Else can maybe be removed?
                        friendInfo =
                            frmGetInfoTest.getInstance()
                                          .database.SummonerInfo.Friends.FirstOrDefault(
                                              f => f.Name == friend.summonerName);
                        if (friendInfo != null && friendInfo.AccountId == 0)
                        {
                            friendInfo.AccountId = JSONdownloader.SummonerProfile(player.Name).acctId;
                            // Add to database
                            // works, but it doesnt use the loaded database, quite stupid
                            frmGetInfoTest.getInstance()
                                          .database.SummonerInfo.Friends.First(w => w.Name == friend.summonerName)
                                          .AccountId =
                                friendInfo.AccountId;
                        }
                    }
                    else
                    {
                        friendInfo.AccountId = JSONdownloader.SummonerProfile(player.Name).acctId;
                    }
                }

                // So Far, So Good

                bool sameMatch = false;
                ElophantAPI.RecentGames.Data matches = JSONdownloader.RecentGames(friendInfo.AccountId.ToString());
                for (int i = 9; i > -1; i--)
                {
                    // Check if premade
                    if (matches.gameStatistics[i].premadeSize > 0)
                    {
                        // For each person in the game, check if in your team, yes? check if it is a friend and if so, add to list.
                        for (int j = 0; j < matches.gameStatistics[i].fellowPlayers.Count; j++)
                        {
                            if (matches.gameStatistics[i].fellowPlayers[j].summonerName != ownSummonerName)
                            {
                                if (GameStatistics.fellowPlayers.FirstOrDefault(
                                    f =>
                                    f.summonerName ==
                                    matches.gameStatistics[i].fellowPlayers[j].summonerName) != null)
                                {
                                    sameMatch = true;
                                }
                                else
                                {
                                    sameMatch = false;
                                    break;
                                }
                            }
                        }
                    }

                    if (sameMatch)
                    {
                        player.Kills = Kills(matches.gameStatistics[i].statistics);
                        player.Assists = Assists(matches.gameStatistics[i].statistics);
                        player.Deaths = Deaths(matches.gameStatistics[i].statistics);

                        player.Score = (player.Kills + (player.Assists/2) - player.Deaths);

                        tempFriendsList.Add(player);
                        break;
                    }
                }
            }
            match.Players = tempFriendsList;
            return match;
        }

        public int Kills(List<ElophantAPI.RecentGames.Statistic> statistics)
        {
            if (statistics.FirstOrDefault(f => f.statType == "CHAMPIONS_KILLED") == null)
            {
                return 0;
            }
            else
            {
                return statistics.FirstOrDefault(f => f.statType == "CHAMPIONS_KILLED").value;
            }
        }

        public int Assists(List<ElophantAPI.RecentGames.Statistic> statistics)
        {
            if (statistics.FirstOrDefault(f => f.statType == "ASSISTS") == null)
            {
                return 0;
            }
            else
            {
                return statistics.FirstOrDefault(f => f.statType == "ASSISTS").value;
            }
        }

        public int Deaths(List<ElophantAPI.RecentGames.Statistic> statistics)
        {
            if (statistics.FirstOrDefault(f => f.statType == "NUM_DEATHS") == null)
            {
                return 0;
            }
            else
            {
                return statistics.FirstOrDefault(f => f.statType == "NUM_DEATHS").value;
            }
        }
    }

    
}